package biolabs.sink;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Multiplexes the pushed data to multiple sinks.
 *
 * <p>The implementation is not thread-safe.</p>
 *
 * @param <T>
 *
 * @author Tomas Mikula
 */
public class MultiplexingSink<T> implements Sink<T> {

	Set<Sink<T>> sinks = new HashSet<Sink<T>>();

	public MultiplexingSink(Sink<T>... sinks) {
		this.sinks.addAll(Arrays.asList(sinks));
	}

	public void addSink(Sink<T> sink) {
		sinks.add(sink);
	}

	public void removeSink(Sink<T> sink) {
		sinks.remove(sink);
	}

	public Collection<Sink<T>> getSinks() {
		return Collections.unmodifiableCollection(sinks);
	}

	@Override
	public void close() {
		for(Sink<T> sink: sinks)
			sink.close();
	}

	@Override
	public void flush() {
		for(Sink<T> sink: sinks)
			sink.flush();
	}

	@Override
	public void push(long time, T data) {
		for(Sink<T> sink: sinks)
			sink.push(time, data);
	}

}
